﻿using System;
using System.Data;
using System.Collections.Generic;
using SERP.DAO;
namespace SERP.Controllers{
public class WorkPositionsController : WorkPositionsControllerBase{
	public WorkPositionsController()
		: base()
	{
	}	
	private DataTable _Table;
	private WorkPositionsEntity oWorkPositions;	
	public WorkPositionsEntity WorkPositionsObject{
		get{
			return oWorkPositions;
		}
		set{
			oWorkPositions=value;
		}
	}
	public DataTable Table{
		get{
			if(_Table==null&&AllowCaching){				
				_Table=Cache.Get<DataTable>(CACHE_KEY_DS);
			}
			return _Table;
		}
		set{
			_Table=value;
		}
	}
	public void LoadAllDSWorkPositions(bool clearCache){
		bool bForce=clearCache;
		if(bForce||_Table==null)
		{
			DataSet ds=Service.SelectAllDS();
			_Table=ds.Tables[0];
			AddIndexToTable(_Table);
			if(AllowCaching)
				AddCache(CACHE_KEY_DS,_Table);			
		}				
	}	
	public void AddIndexToTable(){
		AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		ResetTableIndex(_Table);
	}
	public void LoadObject(Int32 PositionId){
		WorkPositionsEntity oLoad=FindWorkPositions(PositionId);
		if(oLoad==null){
			LoadAllWorkPositions(true);
			oWorkPositions=FindWorkPositions(PositionId);
		}
		else{
			oWorkPositions=oLoad;
		}
	}
	public bool AddNew(){
		if(oWorkPositions==null)return false;
		bool result = Service.Save(oWorkPositions);
		if(result){
			AddObjectToCache(oWorkPositions);
		}
		return result;
	}
	public bool Update(){
		if(oWorkPositions==null)return false;
		return Service.Update(oWorkPositions);	
	}
	public bool Delete(){
		if(oWorkPositions==null)return false;		
		bool retvalue = Service.Delete(oWorkPositions);
		if(retvalue){			
			RemoveObjectFromCache(oWorkPositions);
		}
		return retvalue;
	}	
	public bool DeleteByID(Int32 PositionId){
		bool retvalue = Service.DeleteByID(PositionId);		
		if(retvalue){
			WorkPositionsEntity oDeleted=FindWorkPositions(PositionId);
			RemoveObjectFromCache(oDeleted);
		}
		return retvalue;
	}	
	public void ResetObject(){
		this.oWorkPositions = new WorkPositionsEntity();
	}
	public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.WorkPositionsObject,row);
    }
    public WorkPositionsEntity LoadWorkPositions(Int32 PositionId){			
		WorkPositionsEntity o=Service.SelectOne(PositionId);		
		return o;
	}
	#region Cached
    private static List<WorkPositionsEntity>_ListWorkPositions;
	private static bool _AllowCaching=true;
	public static bool AllowCaching{
		get{return _AllowCaching;}
		set{AllowCaching=value;}
	}	
	public static void ClearCache(){
			if(!_AllowCaching)return;
			Cache.Remove(CACHE_KEY_DS);
			Cache.Remove(CACHE_KEY_LS);
	}
	private const string CACHE_KEY_DS="WorkPositionsDS";
	private const string CACHE_KEY_LS="WorkPositionsLS";
	private void AddObjectToCache(WorkPositionsEntity newObject){
		if(!AllowCaching)return;
		GetCacheList().Add(newObject);
	}
	private void RemoveObjectFromCache(WorkPositionsEntity oRemove)
	{
		if(!AllowCaching)return;
		if(oRemove!=null)
		GetCacheList().Remove(oRemove);
	}
	private static List<WorkPositionsEntity>GetCacheList(){
		if(Cache==null)return default(List<WorkPositionsEntity>);
		List<WorkPositionsEntity>ls= Cache.Get<List<WorkPositionsEntity>>(CACHE_KEY_LS);
		if(ls==null){
			ls = new List<WorkPositionsEntity>();
			AddCache(CACHE_KEY_LS,ls);
		}
		return ls;
	}	
	public static Dictionary<Int32,WorkPositionsEntity> GetWorkPositionsDictionary(){
		Dictionary<Int32,WorkPositionsEntity> dic=new Dictionary<Int32,WorkPositionsEntity>();
		foreach(WorkPositionsEntity o in ListWorkPositionsStatic){
			dic.Add(o.PositionId,o);
		}
		return dic;
	}
	public List<WorkPositionsEntity>ListWorkPositions{
		get{
			return LoadAllWorkPositions(false);
		}
	}
	public static List<WorkPositionsEntity>ListWorkPositionsStatic{
		get{
			LoadAllWorkPositions(false);
			return _ListWorkPositions;
		}
	}
	public WorkPositionsEntity FindWorkPositions(Int32 PositionId){
		if(AllowCaching){
			_ListWorkPositions=GetCacheList();
		}		
		WorkPositionsEntity oResult=_ListWorkPositions.Find(delegate(WorkPositionsEntity oFind){
			bool result=true; 
			result&=oFind.PositionId.Equals(PositionId);
			return result;
		 });
		return oResult;
	}
	public static List<WorkPositionsEntity>LoadAllWorkPositions(bool clearCache){
		_ListWorkPositions = new List<WorkPositionsEntity>();
		if(AllowCaching){
			_ListWorkPositions=GetCacheList();
		}
		if(_ListWorkPositions==null||_ListWorkPositions.Count==0)
			clearCache=true;
		if(!clearCache)return _ListWorkPositions;
		_ListWorkPositions=new List<WorkPositionsEntity>(Service.SelectAll());		
		if(AllowCaching)
			AddCache(CACHE_KEY_LS,_ListWorkPositions);
		return _ListWorkPositions;
	}
	#endregion
}
}
